<?php
class Apaycenter {
	public static function main($funcode) {
		sview::verify(true, true);
		switch ($funcode) {
			case 1:
				self::__doconfig();
				break;
			case 2:
				self::__applepay();
				break;
			case 3:
				self::__billings();
				break;
			case 4:
				self::__payments();
				break;
			case 5:
				self::__androidpay();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}

	//充值配置列表3-7-1
	private static function __doconfig() {
		$paytype = isset(sview::$ctrl['paytype'])? intval(sview::$ctrl['paytype']):2;
		$where   = array('usetype'=>$paytype);
		$field   = 'id as confid,coin,money,app as productid';
		$list    = model::load('live/payconfig')->get_only_list(0, -1, $where, $field, 'coin', orm::ASC);
		sview::$view->assign_by_ref('datasets', $list);
		sview::$view->display();
	}
	
	//ApplePay充值提交3-7-2
	private static function __applepay() {
		loger::getinst()->write('applepay:'.sview::$origin);
		$receipt   = isset(sview::$ctrl['receipt-data'])? sview::$ctrl['receipt-data']:'';
		$transactionid = isset(sview::$ctrl['transactionid'])? sview::$ctrl['transactionid']:'';
		$productid = isset(sview::$ctrl['productid'])? sview::$ctrl['productid']:'';
		$cash      = isset(sview::$ctrl['cash'])? sview::$ctrl['cash']:'';
		if (!$receipt || !$transactionid || !$productid) {
			sview::$view->assign_error(80009, lang::text(80009));
		}
		$payconf  = model::load('live/payconfig')
			->get_item(array('app'=>$productid, 'usetype'=>2));
		if (!$payconf) {
			sview::$view->assign_error(80009, lang::text(80009));
		}
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$user   = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
		if (!$user) {//|| !$user['emailverify']//判断是否已经邮箱验证过了
			sview::$view->assign_error(80068, lang::text(80068));
		}
		$locker = locker::getinst('memcached');
		$queue  = 'apple-pay-'.$userid;
		if (!$locker->lock($queue)) {
			sview::$view->assign_error(80009, lang::text(80009));
		}
		$bzsn   = 'APL'.$transactionid;
		$where  = array('bzsn'=>$bzsn);
		$record = model::load('live/payrecords')->get_item($where);
		$result = api_payment::apple($receipt, $transactionid);
		$memo   = isset($result['memo'])? var_export($result['memo'], true):var_export($result['msg'], true);
		if (!$record) {//未记录的情况下记录资料信息
			$sn     = 'AP'.date('ymdHis').randme::rand_str(6, 2);
			$record = array('userid'=>$userid, 'bzsn'=>$bzsn, 'payconfid'=>$payconf['id'], 'payaccount'=>'', 'paysn'=>$sn, 
				'money'=>$payconf['money'], 'cash'=>$cash, 'coin'=>$payconf['coin'], 'addtime'=>config::$nowtime, 
				'paytime'=>config::$nowtime,'paytype'=>'ApplePay', 'payorigin'=>2, 'refid'=>0, 'token'=>$receipt, 'status'=>0,
				'ip'=>get_ip(), 'memo'=>$memo, 'stime'=>config::$nowtime);
			try {
				$record['id'] = model::load('live/payrecords')->new_one($record);
			} catch (Exception $e) {
				$locker->unlock($queue);
				return;
			}
		}
		//查询结果成功,确定是已经付款的用户
		//$result = api_payment::apple($receipt, $transactionid);
		$orm  = model::load('live/finance')->set_devision($userid);
		$last = 0;
		if ($result['code'] == 0 && $record['status'] == 0) {
			model::load('live/payrecords')->save($record['id'], array('status'=>1));
			$last = api_coin::payment($payconf['coin'], $payconf['money'], $userid, 0);
			api_igguin::add_payment_log($user, $record, 'i');
			//发送邮件提醒付款成功
			if (empty($cash)) {
				$cash = $payconf['money']/100;
			};
			uc_api::mailer($userid, array('nickname'=>$user['nickname'], 'coin'=>$payconf['coin'], 'price'=>$cash,
				'paymethod'=>'ApplePay', 'paysn'=>$record['paysn'], 'datetime'=>api_coreutil::timezone(config::$nowtime)));
		} else {
			$finance = $orm->get_one($userid);
			$last    = $finance['lastcoin'];
		}
		$locker->unlock($queue);
		sview::$view->assign('datasets', array('coin'=>$last));
		sview::$view->assign_by_ref('code', $result['code']);
		sview::$view->assign_by_ref('msg', $result['msg']);
		sview::$view->display();
	}
	
	//AndroidPay充值提交3-7-5
	private static function __androidpay() {
		loger::getinst()->write('googlepay:'.sview::$origin);
		$productid= isset(sview::$ctrl['productid'])? sview::$ctrl['productid']:'';
		$token    = isset(sview::$ctrl['token'])? sview::$ctrl['token']:'';
		$payload  = isset(sview::$ctrl['payload'])? sview::$ctrl['payload']:'';
		$cash     = isset(sview::$ctrl['cash'])? sview::$ctrl['cash']:'';
		if (!$productid || !$token || !$payload) {
			sview::$view->assign_error(80009, lang::text(80009));
		}
		$payconf  = model::load('live/payconfig')
			->get_item(array('app'=>$productid, 'usetype'=>1));
		if (!$payconf) {
			sview::$view->assign_error(80009, lang::text(80009));
		}
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$user   = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
		if (!$user) {//|| !$user['emailverify']//判断是否已经邮箱验证过了
			sview::$view->assign_error(80068, lang::text(80068));
		}
		$locker = locker::getinst('memcached');
		$queue  = 'android-pay-'.$userid;
		if (!$locker->lock($queue)) {
			sview::$view->assign_error(80009, lang::text(80009));
		}
		$bzsn   = 'GPL'.$payload;
		$where  = array('bzsn'=>$bzsn);
		$record = model::load('live/payrecords')->get_item($where);
		$result = api_payment::android($productid, $token);
		$memo   = ($result['code'] == 0)? $result['developerPayload']:$result['msg'];
		if (!$record) {//未记录的情况下记录资料信息
			$sn     = 'GL'.date('ymdHis').randme::rand_str(6, 2);
			$record = array('userid'=>$userid, 'bzsn'=>$bzsn, 'payconfid'=>$payconf['id'], 'payaccount'=>'', 'paysn'=>$sn,
				'money'=>$payconf['money'], 'coin'=>$payconf['coin'], 'addtime'=>config::$nowtime, 'paytime'=>config::$nowtime,
				'paytype'=>'GooglePay', 'payorigin'=>3, 'refid'=>0, 'token'=>$token, 'status'=>0, 'ip'=>get_ip(),
				'memo'=>$memo, 'stime'=>config::$nowtime);
			try {
				$record['id'] = model::load('live/payrecords')->new_one($record);
			} catch (Exception $e) {
				$locker->unlock($queue);
				return;
			}
		}
		//查询结果成功,确定是已经付款的用户
		$orm  = model::load('live/finance')->set_devision($userid);
		$last = 0;
		if ($result['code'] == 0 && $record['status'] == 0) {
			model::load('live/payrecords')->save($record['id'], array('status'=>1));
			$last = api_coin::payment($payconf['coin'], $payconf['money'], $userid, 0);
			api_igguin::add_payment_log($user, $record, 'a');
			//发送邮件提醒付款成功
			if (empty($cash)) {
				$cash = $payconf['money']/100;
			};
			uc_api::mailer($userid, array('nickname'=>$user['nickname'], 'coin'=>$payconf['coin'], 'price'=>$cash,
					'paymethod'=>'GooglePay', 'paysn'=>$record['paysn'], 'datetime'=>api_coreutil::timezone(config::$nowtime)));
		} else {
			$finance = $orm->get_one($userid);
			$last    = $finance['lastcoin'];
		}
		$locker->unlock($queue);
		sview::$view->assign('datasets', array('coin'=>$last));
		sview::$view->assign_by_ref('code', $result['code']);
		sview::$view->assign_by_ref('msg', $result['msg']);
		sview::$view->display();
	}
	
	//消费账单信息 3-7-3
	private static function __billings() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$start  = (int)sview::$ctrl['offset'];
		$limit  = 25;
		$where  = array('userid'=>$userid);
		$fields = 'anchorid,reftype,refid,coinprice,nums,stime';
		$list   = model::load('live/moneyflow')->get_only_list($start, $limit, $where, $fields, 'id', orm::DESC);
		if ($list) {
			$uorm = model::load('live/userinfo');
			$gorm = model::load('live/gift');
			$dorm = model::load('live/guard');
			foreach ($list as &$item) {
				$item['nickname'] = lang::text(60037);
				if ($item['anchorid'] > 0) {
					$user = $uorm->set_devision($item['anchorid'])->get_one($item['anchorid']);
					if ($userid != $item['anchorid']) {
						$item['nickname'] = htmlspecialchars_decode($user['nickname']);
					}
				}
				if ($item['reftype'] == 1) {
					$gift 		     = $gorm->get_one($item['refid']);
					$item['refname'] = $gift['name'];
					$item['iconsrc'] = $gift['iconsrc'];
				} else if ($item['reftype'] == 4) {
					$guard 			 = $dorm->get_one($item['refid']);
					$item['refname'] = $guard['name'];
					$item['iconsrc'] = $row['iconsrc'];
				} else {
					$item['refname'] = 'UnKnow';
					$item['iconsrc'] = '';
				}
			}
		}
		sview::$view->assign_by_ref('datasets', $list);
		sview::$view->display();
	}
	
	//充值账单信息 3-7-4
	private static function __payments() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$start  = (int)sview::$ctrl['offset'];
		$limit  = 25;
		$where  = array('userid'=>$userid);
		
		$config = config::load('psocial', false);
		$field  = 'id,paysn,addtime as stime,money,cash,coin,paytype,token,status';
		$list   = model::load('live/payrecords')->get_only_list($start, $limit, $where, $field, 'id', orm::DESC);
		sview::$view->assign_by_ref('datasets', $list);
		sview::$view->display();
	}
}